package offer;

/**
 * @Author Elephas
 * @Date 2022/3/19
 **/
@FunctionalInterface
public interface SortColors {
    void sortColors(int[] nums);
}

class SortColorsImpl1 implements SortColors{
    static final int RED = 0;
    static final int WHITE = 1;
    static final int BLUE = 2;
    @Override
    public void sortColors(int[] nums) {
        int n = nums.length;
        int left = -1, right = n;
        for (int i = 0; i < n; i++) {
            if(nums[i] == RED){
                left++;
                swap(nums, i, left);
            }else if(nums[i] == BLUE){
                right--;
                swap(nums, i, right);
                i--;
            }
        }
    }
    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}